using System;
using System.Windows.Forms;
using System.Drawing;

namespace YetAnotherTestProject
{
    class MyFlow : UserControl
    {
        public MyFlow()
        {
            //this.AutoScroll = true;
            this.BackColor = Color.Green;
        }

        protected override void OnPaddingChanged(EventArgs e)
        {
            this.Redraw();
            base.OnPaddingChanged(e);
        }

        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlAdded(e);
            this.Redraw();

            e.Control.Visible = true;

            e.Control.Click += new EventHandler(Control_Click);
        }

        void Control_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Top: {0}", (sender as Control).Top);
        }

        protected override void OnControlRemoved(ControlEventArgs e)
        {
            this.Redraw();
            base.OnControlRemoved(e);
        }

        protected override void OnResize(EventArgs e)
        {
            this.Redraw();
            base.OnResize(e);
        }

        private void Redraw()
        {
            for(int i = 0; i < this.Controls.Count; i++)
            {
                this.Controls[i].Location = new Point(Math.Max(this.Padding.Left, (this.ClientSize.Width / 2) - (this.Controls[i].Width / 2)), 
                    (i * (this.Controls[i].Height + this.Padding.Vertical)) + this.Padding.Top + this.AutoScrollPosition.Y);
            }

            Console.WriteLine("VScrollPos: {0}", this.VerticalScroll.Value);
            //int scrollPos = this.VerticalScroll.Value;
            this.AutoScrollMinSize = new Size(this.Padding.Horizontal + 128, this.Controls.Count * (this.Padding.Vertical + 96));
            //this.VerticalScroll.Value = scrollPos;
        }
    }
}
